//
//  LXCommissionViewController.m
//  LvXiShopping
//
//  Created by 诸葛孔明 on 16/5/27.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXCommissionViewController.h"

#import "LXCommissionHeaderView.h"

#import "LXCommissionTeamController.h"

#import "LXCommissionDetailTBVC.h"

#import "LXCommissionOptionCell.h"

#import "LXMallShareController.h"



@interface LXCommissionViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView * tableView;

@end

@implementation LXCommissionViewController

NSString * const commissionCellID = @"CommissionCell";

NSString * const LXCommissionOptionCellID = @"LXCommissionOptionCell";


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBColor(234, 234, 234);
    
    [self setUpTableView];
    
}

-(void)setUpNavi{
    [super setUpNavi];
    self.navigationItem.title = @"我的分佣";
}
-(void)backButtonClick:(UIButton*)button{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)setUpTableView{
    UITableView * tableView   = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, ScreenSize.width-20, ScreenSize.height) style:UITableViewStyleGrouped];
    tableView.backgroundColor = [UIColor clearColor];
    tableView.delegate        = self;
    tableView.dataSource      = self;
    tableView.sectionFooterHeight = 40;
    tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:tableView];

    LXCommissionHeaderView * headerView = [[LXCommissionHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, tableView.width*0.9)];
    tableView.tableHeaderView = headerView;
    
    
    [tableView registerClass:[LXCommissionOptionCell class] forCellReuseIdentifier:LXCommissionOptionCellID];
    
    self.tableView            = tableView;

}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setUpNavi];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray * titles = @[@"我的团队",@"佣金明细",@"推广赚钱"];
    NSArray * images = @[@"brokerage_but_team",@"brokerage_but_detail",@"brokerage_but_money"];
    LXCommissionOptionCell * cell = [tableView dequeueReusableCellWithIdentifier:LXCommissionOptionCellID];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = titles[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:images[indexPath.row]];
    [self addCellHeight:cell.cellHeight indexPath:indexPath];
    cell.backgroundImage = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(tableView.width, cell.cellHeight)];
    cell.enableUnderLine = YES;
    if(indexPath.row == 2){
        cell.backgroundImage = [UIImage imageNamed:@"brokerage_bg02"];
        cell.enableUnderLine = NO;
    }
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, tableView.width, 40)];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setTitle:@"佣金规则" forState:UIControlStateNormal];
    button.width = [button.currentTitle sizeInAre:MaxSize font:button.titleLabel.font].width+20;
    button.height = [button.currentTitle sizeInAre:MaxSize font:button.titleLabel.font].height;
    
    [button setImage:[UIImage imageNamed:@"small_back_data_icon"] forState:UIControlStateNormal];
    [button setTitleColor:RGBColor(146, 146, 146) forState:UIControlStateNormal];
    button.imageEdgeInsets = UIEdgeInsetsMake(0, button.width-20, 0, -(button.width-20));
    button.titleEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
    [button addTarget:self action:@selector(pushToRulerController1) forControlEvents:UIControlEventTouchUpInside];
    return button;
}


-(void)pushToRulerController1{
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self cellHeightForIndexPath:indexPath];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        LXCommissionTeamController * vc = [[LXCommissionTeamController alloc] init];
        vc.showSubTeam = YES;
        vc.subName = @"我的团队";
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if(indexPath.row == 1){
        LXCommissionDetailTBVC * detailTBVC = [[LXCommissionDetailTBVC alloc] initWithStyle:UITableViewStyleGrouped];
        [self.navigationController pushViewController:detailTBVC animated:YES];
        
    }
    
    if(indexPath.row == 2){
        LXMallShareController * shareController = [[LXMallShareController alloc] init];
        [self.navigationController pushViewController:shareController animated:YES];
    }
}





-(void)dealloc{
    NSLog(@"%s",__func__);
}



//设定table view 分割线 贯穿整个屏幕
-(void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,-1,0,1)];
    }

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,-1,0,1)];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

@end
